import java.util.*;
import java.text.*;

/**
 * Booking is used within a room and contains the booking name, start date/time
 * and end date/time of the booking and a formatter for printing the date.
 */

public class Booking implements Comparable<Object>
{
    private String bookingName;
    private Date startTime;
    private Date endTime;

    // Used to format printing dates for files
    private SimpleDateFormat dFormat;

    // Constructor
    /**
     * Constructor initialises the formatter so dates can be printed in the 
     * form DD/MM/YYYY,hh:mm.
     */
    public Booking()
    {
        dFormat = new SimpleDateFormat("dd/MM/yyyy,HH:mm");
    }

    // Getters

    /**
     * Returns the booking name.
     */
    public String getBookingName()
    {
        return bookingName;
    }

    /**
     * Returns the start date/time.
     */
    public Date getStartTime()
    {
        return startTime;
    }

    /**
     * Returns the end date/time.
     */
    public Date getEndTime()
    {
        return endTime;
    }

    // Setters

    /**
     * Sets the booking name.
     *
     * @param name booking name
     */
    public void setBookingName(String name)
    {
        bookingName = name;
    }

    /**
     * Sets the booking start date/time.
     *
     * @param date start date/time
     */
    public void setStartTime(Date date)
    {
        startTime = date;
    }

    /**
     * Sets the booking end date/time. If the end date/time entered is
     * before the start/date time, the user will be asked to enter a new
     * date.
     *
     * @param date end date/time
     */
    public void setEndTime(Date date)
    {
        if (date.after(startTime))
        {
            endTime = date;
        }
        else
        {
            System.out.print("Please enter end time after start time: ");
            setEndTime(Read.date(""));
        }
    }

    // Functions

    // Display functions

    /**
     * Print start time in DD/MM/YY,hh:mm format.
     */
    public String printStartTime()
    {
        return dFormat.format(startTime.getTime());
    }

    /**
     * Print end time in DD/MM/YY,hh:mm format.
     */
    public String printEndTime()
    {
        return dFormat.format(endTime.getTime());
    }

    /** 
     * Checks for a booking clash with the current booking. A clash is
     * present only if bookings overlap. This means that for a particular day,
     * if the start time of one booking matches the end time of another, the
     * isClash() function will return false.
     * 
     * @param newStartTime start date/time of proposed booking
     * @param newEndTime end date/time of proposed booking
     */
    public boolean isClash(Date newStartTime, Date newEndTime)
    {
        boolean check = false;

        // Tests all possibilities of clashing
        if ((newStartTime.equals(startTime)) 
            || (newEndTime.equals(endTime))
            || (newStartTime.after(startTime) && newStartTime.before(endTime))
            || (newEndTime.after(startTime) && newEndTime.before(endTime))
            || (newStartTime.before(startTime) && newEndTime.after(endTime))
        )
        {
            check = true;
        }

        return check;
    }

    /** 
     * Used to compare booking on start time.
     * 
     * @return -1,0,1 when Booking o starts before, at the same time or after
     * this booking
     */
    public int compareTo(Object o) throws ClassCastException
    {
        int value = 0;

        if(o instanceof Booking)
        {
            Booking booking = (Booking) o;

            if(this.getStartTime().after(booking.getStartTime())) 
            {
                value = 1;
            }
            else if(this.getStartTime().before(booking.getStartTime()))
            {
                value = -1;
            }
        }

        return value;
    }
}
